package com.viphuli.app.tool.fragment;

import android.app.Activity;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import butterknife.Bind;
import butterknife.OnClick;

import com.loopj.android.http.RequestParams;
import com.viphuli.app.tool.R;
import com.viphuli.app.tool.bean.part.AccountRemind;
import com.viphuli.app.tool.common.AccessTokenKeeper;
import com.viphuli.app.tool.common.MyPageHelper;
import com.viphuli.app.tool.handler.MyBaseHttpResponseHandler.HandlerType;
import com.viphuli.app.tool.http.ApiRequest;


public class AccountRemindFragment extends BaseProgressFragment {

	@Bind(R.id.id_account_remind_switch)
	protected View remindSwitchView;

	@Bind(R.id.id_account_remind_msg)
	protected EditText remindMsgView;

	@Bind(R.id.id_account_remind_time_desc)
	protected TextView timeDescText;

	@Bind(R.id.id_account_remind_time)
	protected View remindTimeView;

	private AccountRemind remind;

	private int hour;
	private int minute;

	@Override
	protected int obtainContentRes() {
		return R.layout.frag_account_remind;
	}

	@Override
	protected void initParams() {

	}

	@Override
	protected void initView() {

	}

	@Override
	protected void initData() {

		AccessTokenKeeper tokenKeeper = AccessTokenKeeper.readAccessToken(getActivity());

		if (tokenKeeper.isLogin()) {

			RequestParams params = new RequestParams();
			params.put("uid", tokenKeeper.getOpenId());
			ApiRequest.accountRemindInfo.request(this, params);

		}

	}

	@OnClick(R.id.id_account_remind_switch)
	public void clickSwitch(View v) {
		AccessTokenKeeper tokenKeeper = AccessTokenKeeper.readAccessToken(getActivity());

		boolean switching = false;
		boolean repeat = false;
		String msg = "";

		if (remind != null) {
			switching = remind.isSwitching() ? false : true;
			msg = remind.getMsg();
		}

		if (tokenKeeper.isLogin() || remind != null) {

			RequestParams params = new RequestParams();
			params.put("uid", tokenKeeper.getOpenId());
			params.put("switch", switching);
			params.put("repeat", repeat);
			params.put("msg", msg);
			params.put("ahead_minutes", remind.getAheadMinutes());

			if (switching) {
				remindTimeView.setVisibility(View.VISIBLE);
			} else
				remindTimeView.setVisibility(View.GONE);

			ApiRequest.accountChangeRemindInfo.request(this, params, HandlerType.none);

		}
	}

	@OnClick(R.id.id_account_remind_time)
	public void clickRemindTime(View v) {

		AccountRemindTimeDialog dialogFrag = new AccountRemindTimeDialog();
		dialogFrag.setRemind(remind);
		dialogFrag.show(getChildFragmentManager(), dialogFrag.getClass().getSimpleName());

	}

	@Override
	public void onBackPressed() {

		boolean switching = false;
		String msg = "";

		if (remind != null) {
			switching = remind.isSwitching() ? true : false;
			msg = remind.getMsg();
		}

		if (!msg.equals(remindMsgView.getText().toString())) {

			AccessTokenKeeper tokenKeeper = AccessTokenKeeper.readAccessToken(getActivity());

			if (tokenKeeper.isLogin()) {

				RequestParams params = new RequestParams();
				params.put("uid", tokenKeeper.getOpenId());
				params.put("switch", switching);
				params.put("repeat", false);
				params.put("ahead_minutes", remind.getAheadMinutes());
				params.put("msg", remindMsgView.getText().toString());

				ApiRequest.accountChangeRemindInfo.request(this, params, HandlerType.none);

			}

		}

		getActivity().finish();

	}

	public View getRemindSwitchView() {
		return remindSwitchView;
	}

	public TextView getRemindMsgView() {
		return remindMsgView;
	}
	
	public View getRemindTimeView() {
		return remindTimeView;
	}

	public void setRemind(AccountRemind remind) {
		this.remind = remind;
	}

	public AccountRemind getRemind() {
		return this.remind;
	}

	public void setHour(int hour) {
		this.hour = hour;
	}

	public void setMinute(int minute) {
		this.minute = minute;
	}

	public TextView getTimeDescText() {
		return timeDescText;
	}

	public static void go(Activity act) {
		MyPageHelper.accountRemind.showMyPage(act);
	}

}
